package de.baumann.browser.activitys.gg

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import de.baumann.browser.BuildConfig
import de.baumann.browser.R
import de.baumann.browser.activitys.user.BindETHActivity
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.IBaseView
import de.baumann.browser.iview.gg.ICreateSmelterView
import de.baumann.browser.present.gg.CreateSmelterPresenter
import de.baumann.browser.utils.StringUtil
import de.baumann.browser.utils.getUser
import de.baumann.browser.utils.web3j.Erc20Util.transferERC20Token2
import de.baumann.browser.utils.web3j.TokenUtil
import de.baumann.browser.utils.web3j.WalletDaoUtils
import de.baumann.browser.views.dialog.BindEthDialog
import de.baumann.browser.views.dialog.PayPwdDialog
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_create_smelter.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import java.lang.Exception

class CreateSmelterActivity : BaseOdinActivity<ICreateSmelterView, CreateSmelterPresenter>(), ICreateSmelterView {

    companion object {
        fun startCreateSmelterActivity(context: Context, wallet: ETHWallet) {
            context.startActivity(Intent(context, CreateSmelterActivity::class.java).apply {
                putExtras(Bundle().apply {
                    putSerializable("wallet", wallet)
                })
            })
        }
    }

    override fun getTitleText(): String {
        return "Create smelter"
    }


    override fun getLayout(): Int {
        return R.layout.activity_create_smelter
    }

    private lateinit var payPwdDialog: PayPwdDialog
    override fun initView() {
        initToolbar(commToolBar, commTitle)
        btnCreateSmelter.setOnClickListener {
            presenter?.checkName()
        }
    }

    override fun initData() {
        presenter?.getRule()
    }

    override fun createPresenter(): CreateSmelterPresenter {
        return CreateSmelterPresenter()
    }

    override fun createView(): ICreateSmelterView {
        return this
    }

    override fun getSmelterName(): String {
        return SmelterName.text.toString().trim()
    }

    override fun getMortgageAmount(): String {
        return "200000"
    }

    override fun getMortgageTime(): Long {
        return 12
    }

    override fun transferSuccess() {
        payPwdDialog.dismiss()
        showShortToast("Create Success")
//        MySmelterActivity.startMySmelterActivity(mContext)
        finish()
    }

    override fun setRule(rule: String) {
        tvRule.text = rule
    }

    override fun namePass(pass: Boolean) {
        if (!pass) {
            showShortToast("Smelter name already exists")
            return
        }
        payPwdDialog = PayPwdDialog(mContext, object : PayPwdDialog.PwdCallback {
            override fun callback(pwd: String) {
                try {
                    val privateKey = TokenUtil.getPrivateKey(pwd, getWallet())
                    presenter.createSmelterTransferToken(getWallet().address, privateKey)
                } catch (e: Exception) {
                    if ("Invalid password provided" == e.message) {
                        Toast.makeText(mContext, "Wrong Password", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(mContext, e.message, Toast.LENGTH_SHORT).show()
                    }
                }
            }
        })
        payPwdDialog.show()
    }

    private fun getWallet(): ETHWallet {
        return intent.getSerializableExtra("wallet") as ETHWallet
    }

}